package com.ibm.csorb.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.ibm.csorb.entity.Link;
import com.ibm.csorb.entity.Menu;

@Controller
// @SessionAttributes("currUser")
public class LoginController {

	@RequestMapping(value = "/login")
	public String login(String username, String password, Model model) {
		System.out.println("loginzcqf...." + username);
		model.addAttribute("currUser", username);
		return "home";
	}

	@RequestMapping(value = "/logout")
	public String logout(@ModelAttribute("currUser") String username,
			SessionStatus status) {
		System.out.println("logout...." + username);
		status.setComplete();
		return "redirect:/index.jsp";
	}

	@RequestMapping(value = "/viewpage")
	public String viewPage(@ModelAttribute("currUser") String username,
			String page) {
		if (username == null || username.equals("")) {
			return "redirect:/index.jsp";
		}
		return page;
	}

	@RequestMapping(value = "/getmenulist")
	public ModelAndView getMenuList() {
		ModelAndView mv = new ModelAndView("jsonView");
		List<Menu> menus = new ArrayList<Menu>();
		List<Link> links = new ArrayList<Link>();
		Menu menu = new Menu("id1", "menu1");
		Menu menu2 = new Menu("id2", "menu2");
		Menu menu3 = new Menu("id3", "menu3");
		Link l1 = new Link("link1", "link1", "www.baidu.com", "link1");
		Link l2 = new Link("link2", "link2", "www.baidu.com", "link2");
		links.add(l1);
		links.add(l2);
		menu.setLinks(links);
		menus.add(menu);
		menus.add(menu2);
		menus.add(menu3);
		mv.addObject("menuroot", menus);
		// mv.addObject(menus);
		return mv;
	}

	public ModelMap getMenuList_bak() {
		StringBuffer xmlmenu = new StringBuffer();
		PathMatchingResourcePatternResolver resolover = new PathMatchingResourcePatternResolver();
		Resource menuFile = resolover.getResource("classpath:left-menu.xml");
		try {
			File xmlFile = menuFile.getFile();
			BufferedReader br = new BufferedReader(new FileReader(xmlFile));
			String line = null;
			while ((line = br.readLine()) != null) {
				xmlmenu.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		ModelMap model = new ModelMap();
		model.addAttribute("", xmlmenu.toString());

		return model;
	}

}
